package Case;
//同步代方法演示
//需求：假如电影院共有100张电影票需要出售，在门口一共有三台售票机同时售票，请设计一款程序模拟售票
public class Demo2 {
    static int ticket=0;//声明100张票,设为静态属性,确保只有一份
    public static void main(String[] args) {
        Runnable sales=()-> {
         while (true){
             if(function()) break;
         }
        };
    new Thread(sales,"机器1号").start();
    new Thread(sales,"机器2号").start();
    new Thread(sales,"机器3号").start();
    }
    public static synchronized Boolean function() {
        Thread.yield();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if (ticket!=100) {
            ticket++;
            String name = Thread.currentThread().getName();
            System.out.println(name + "售出:" + ticket + "号票");
        } else {
            return true;
        }
        return false;
    }


}
